<template>
	<view>
		<!-- 顶部标题 -->
		<!-- <view class="head_text">
			<text>新增知识库</text>
			<image src="../../static/client/back.png"
				style="width: 20rpx;height: 36rpx;position: absolute;left: 30rpx;top: 63rpx;" @click="back()"></image>
		</view> -->
		<view class="containerForAddKnow">
			<view>
				<!-- 标题 -->
				<view class="title">
					<text class="all_text_title">标题</text>
					<input style="margin-top: 39rpx;" placeholder="单行输入" v-model="addFormData.title"
						placeholder-style="color:#999999;" />
				</view>
				<!-- 相关项目类型 -->
				<view class="title" style="height: auto;padding-bottom:20rpx; min-height: 152rpx;">
					<text class="all_text_title">相关项目类型</text>
					<checkbox-group @change="handleChange" v-model="addFormData.project_type"
						style="margin-left: 197rpx;margin-top: -70rpx;display: flex;flex-direction: column;">
						<label style="margin-top: 30rpx;" v-for="item in projectTypeList" :key='item.project_type_id'>
							<checkbox :value="item.project_type_id" :checked="item.checked"
								style="transform: scale(0.7);" />
							<text>{{item.project_type_name}}</text>
						</label>
					</checkbox-group>
				</view>
				<!-- 内容 -->
				<view class="title" style="height: auto; border-bottom: 0;">
					<text class="all_text_title">内容</text>
					<textarea v-model="addFormData.content"
					maxlength="-1"
					style="width: 690rpx;height: 305rpx;background: #F7F7F7;margin-top: 30rpx;"
						 />
				</view>
				<!-- 附件上传-->
				<view class="titlemodify" style="border-bottom: 0; position: relative;">
					<text class="all_text_title">相关附件</text>
					<l-file ref="lFile" @up-success="onSuccess"></l-file>
					<view style="position: absolute;top: 0; left: 177rpx; z-index:999" @click="handleUpLoad">
						<image src="../../static/firm/icon_sc.png" style="width: 25rpx; height: 19rpx;">
						</image>
						<text style="font-size: 20rpx; color: #128BF1;margin-left: 10rpx;">点击上传文件</text>
					</view>
					<view class="upLoadFileWzh" style="border-bottom: 0;position: relative;"
						v-for="item in addFormData.annex" :key='item.url'>
						<view class="left">
							<image src="../../static/know/PDF.png" style="width: 40rpx; height: 48rpx;display: block;">
							</image>
							<view @click="checkDetails(item)" class="file_name">{{item.name}}</view>
						</view>
						<uni-icons @click.stop="handleRemoveFile(item)" type="close" color='#eee' size="20"></uni-icons>
					</view>
				</view>

				<!-- 底部按钮 -->
				<view class="bottom">
					<button class="bottom_btn" @click="handleCancel" style="background: #CCCCCC; width: 380rpx;" form-type="reset">取消</button>
					<button class="bottom_btn"
						style="background: linear-gradient(0deg, #037CE3, #2EB3FF); margin-top: -88rpx;margin-left: 50%;"
						@click="handleSubmit" form-type="submit">确定</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				projectTypeList: [],
				addFormData: {
					title: '',
					content: '',
					project_type: [],
					annex: [],
				},
			}
		},
		async onLoad() {
			this.token = uni.getStorageSync('btlh_process_token') || ''
			this.member_id = uni.getStorageSync('btlh_process_member_id') || ''
			this.company_id = uni.getStorageSync('bthl_process_LoginCompanyId') || ''
			this.getProjectTypeList()
		},
		methods: {
			toReport(path) {
				path = path || 'http.......slfjlf.pdf'
				let first = path.lastIndexOf("."); //取到文件名开始到最后一个点的长度
				let suffixName = path.substring(first, path.length); //截取获得后缀名
				uni.showLoading({
					title: '加载中，请稍后~'
				})
				const fs = uni.getFileSystemManager()
				// 获取本地缓存文件清空，防止缓存过多预览失败
				fs.readdir({
					dirPath: wx.env.USER_DATA_PATH,
					success: res => {
						if (res.files && res.files.length > 0) {
							res.files.forEach(item => {
								fs.unlink({
									filePath: wx.env.USER_DATA_PATH + '/' + item
								})
							})
							this.openDocument(path, suffixName)
						} else {
							this.openDocument(path, suffixName)
						}
					}
				})
			},
			openDocument(path, suffixName) {
				uni.downloadFile({
					url:  path,
					filePath: wx.env.USER_DATA_PATH + '/' + + suffixName,
					success: res => {
						uni.hideLoading()
						console.log('准备预览')
						console.log(res)
						uni.openDocument({
							showMenu: true,
							filePath: res.filePath,
						})

					},
					fail: res => {
						uni.hideLoading()
						console.log(res)
					}
				})
			},
			// 预览文件
			checkDetails(item) {
				console.log(item.url)
				uni.showLoading({
					title: '加载中，请稍后~'
				})
				uni.downloadFile({
					url: item.url.replace("http:","https:"), //仅为测试接口，并非真实的
					success: function(res) {
						// console.log(res)
						// return
						var postf1 = item.type
						var filePath = res.tempFilePath
						uni.openDocument({
							filePath: filePath,
							fileType: postf1,
							success: function(res) {
								console.log('预览res',res);
								uni.hideLoading()
							},
							fail: function(res) {
								console.log('fail萨达的萨萨达萨达萨')
								console.log(res)
								uni.hideLoading()
							},
							complete(res) {
								console.log(res);
								console.log('预览rescccccc',res);
								uni.hideLoading()
							}
						});
					},
					fail: function(res) {
						console.log('fail')
						console.log(res)
					},
					complete: function(res) {
						console.log('complete')
						console.log(res)
					}
				})
			},
			handleUpLoad() {
				this.$refs.lFile.upload({
					// #ifdef APP-PLUS
					currentWebview: this.$mp.page.$getAppWebview(),
					// #endif
					//非真实地址，记得更换
					url: 'https://control.sxpz.vip/index.php/AppApi/User/uploadImg',
					//默认file,上传文件的key
					name: 'file',
					member_id: this.member_id,
					token: this.token,
					//...其他参数
				});
			},
			onSuccess(res) {
				console.log(res)
				let ress = res.data
				let fileName = res.fileName
				if (ress.code != 200) return uni.showToast({
					title: ress.datas.msg,
					icon: 'none'
				})
				uni.showToast({
					title: '上传成功',
					icon: 'none'
				})
				let nameArr = ress.datas.img_url.split('/')
				let typeArr = ress.datas.img_url.split('.')
				this.addFormData.annex.push({
					name: fileName,
					type: typeArr[typeArr.length - 1],
					url: ress.datas.img_url,
				})

			},
			// 删除文件
			handleRemoveFile(val) {
				let removeIndex = this.addFormData.annex.findIndex(
					(item) => item.url === val.url
				)
				console.log(removeIndex)
				this.addFormData.annex.splice(removeIndex, 1)
			},
			handleChange(e) {
				// console.log(this.addFormData.project_type)
				console.log(this.projectTypeList)
				console.log('e', e.detail.value)
				this.addFormData.project_type = e.detail.value
			},
			// 提交新增
			async handleSubmit() {
				const {
					data: res
				} = await this.$myRequest({
					url: 'Knowledge/add',
					method: 'POST',
					data: {
						member_id: this.member_id,
						token: this.token,
						company_id: this.company_id,
						title: this.addFormData.title,
						content: this.addFormData.content,
						project_type: this.addFormData.project_type.join(','),
						annex: JSON.stringify(this.addFormData.annex),
					},

				})
				if (res.code != 200) return uni.showToast({
					title: res.datas.msg,
					icon: 'none'
				})
				uni.showToast({
					title: res.datas.msg,
					icon: 'none'
				})

				uni.switchTab({
					url: `/pages/know/know`
				})
			},
			handleCancel(){
				uni.switchTab({
					url: `/pages/know/know`
				})
			},
			async getProjectTypeList() {
				const {
					data: res
				} = await this.$myRequest({
					url: 'Project/project_type',
					method: 'POST',
					data: {
						member_id: this.member_id,
						token: this.token,
						company_id: this.company_id,

					},

				})
				if (res.code != 200) return uni.showToast({
					title: res.datas.msg,
					icon: 'none'
				})
				this.projectTypeList = res.datas.listInfo
				console.log('项目类型', this.projectTypeList)
			},
			/* 
			返回按钮 
			 */
			back() {
				uni.navigateBack({
					delta: 1
				})
			}
		}
	}
</script>

<style>
	.title {
		width: 690rpx;
		height: 152rpx;
		margin-top: 44rpx;
		margin-left: 30rpx;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #333333;
		border-bottom: 1rpx solid #E3E3E3;
	}



	.head_text {
		width: 100%;
		height: 128rpx;
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #333333;
		line-height: 160rpx;
		text-align: center;
		border-bottom: 19rpx solid #F7F7F7;
	}

	.bottom {
		width: 100%;
		height: 88rpx;
		position: fixed;
		bottom: 0;
		z-index: 10000;
	}

	.bottom_btn {
		width: 375rpx;
		height: 88rpx;
		margin-left: 0;
		border-radius: 0;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
	}

	.containerForAddKnow {
		height: calc(100% - 128rpx);
		box-sizing: border-box;
		padding-bottom: 120rpx;
		overflow-y: auto;
	}

	/* 文件 */
	.titlemodify {
		width: 690rpx;
		margin-top: 44rpx;
		margin-left: 30rpx;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #333333;
		border-bottom: 1rpx solid #E3E3E3;
	}


	.upLoadFileWzh {
		padding-left: 20rpx;
		margin-top: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.file_name {
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: 500;
		width: 490rpx;
		overflow: hidden;
		/*超出部分隐藏*/
		white-space: nowrap;
		/*不换行*/
		text-overflow: ellipsis;
		/*超出部分文字以...显示*/
		color: #999999;
		margin-left: 30rpx;
	}

	.upLoadFileWzh .left {
		display: flex;
		justify-content: flex-start;
		align-items: center;
	}
</style>
